package com.woniu40.petai_user.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu40.petai_user.entity.Attention;
import com.woniu40.petai_user.entity.Dynamic;
import com.woniu40.petai_user.mapper.AttentionMapper;
import com.woniu40.petai_user.mapper.DynamicMapper;
import com.woniu40.petai_user.vo.DynamicVo;
import com.woniu40.petai_user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu40.petai_user.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;
import com.woniu40.petai_user.entity.User;

@Service
public class DynamicService {

    @Autowired
    DynamicMapper dynamicMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AttentionMapper attentionMapper;


    public List<User> findAttentionUsers(Integer userId){
        //获取关注量排名前5的用户ID
        List<Integer> userIds = dynamicMapper.findAttentionUserId(userId);
        //根据id查询用户信息
        List<User> users = userMapper.selectByIds(userIds);
        return users;
    }

    public List<Dynamic> selectAll(Integer uid){
        List<Dynamic> dynamics = dynamicMapper.selectAll(uid);
        return dynamics;

    }



    public PageInfo<DynamicVo> findDynamicByPage(Integer pageNum, Integer pageSize,
                                                 Integer plateId, String like,
                                                 Integer order){
        List<DynamicVo> dynamicVocList = new ArrayList<>();
        if(plateId!=3){
            PageHelper.startPage(pageNum,pageSize);
            dynamicVocList = dynamicMapper.selectByTips(plateId,like,order);
        }else{
            dynamicVocList = dynamicMapper.selectByTips2(like,order);
        }

        return new PageInfo<>(dynamicVocList);
    }

    public int updataViewNum(Integer dynamicId){
        int i = dynamicMapper.updataViewNum(dynamicId);
        return i;
    }

    public int addDynamic(Dynamic dynamic){
        int insert = dynamicMapper.insert(dynamic);
        return insert;
    }

    public User findUser(Integer userId){
        User user = userMapper.selectByPrimaryKey(userId);
        return user;

    }

    public List<Dynamic> findDynamicsByUserId(Integer userId){
        List<Dynamic> list = dynamicMapper.selectDynamicByUserId(userId);
        return list;
    }

    public Attention isAttention(Integer userId,Integer attentionUserId){
        Attention attention = attentionMapper.select(userId, attentionUserId);
        return attention;
    }

    public int addAttention(Attention attention){
        int insert = attentionMapper.insert(attention);
        return insert;
    }

    public int removeAttention(Integer userId,Integer attentionUserId){
        int delete = attentionMapper.delete(userId, attentionUserId);
        return delete;
    }
    public Dynamic findDynamic(Integer DynamicId){
        Dynamic dynamic = dynamicMapper.selectByPrimaryKey(DynamicId);
        return dynamic;

    }




}
